package com.example.pufang.controller.front;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.common.CommonResult;
import com.example.common.PageParamRequest;
import com.example.exception.ExceptionCodeEnum;
import com.example.pufang.pojo.Feedback;
import com.example.pufang.pojo.SystemMessage;
import com.example.pufang.pojo.User;
import com.example.pufang.service.FeedbackService;
import com.example.pufang.service.SystemMessageService;
import com.example.pufang.service.UserService;
import com.example.pufang.vo.FeedbackFrontListVo;
import com.example.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/front/feedback")
@CrossOrigin
@Api(tags = "h5用户反馈接口")
public class FeedbackFrontController {

    @Autowired
    private FeedbackService feedbackService;

    @Autowired
    private UserService userService;

    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private SystemMessageService systemMessageService;

    @Operation(summary = "获取反馈列表")
    @GetMapping("list")
    public CommonResult<List<FeedbackFrontListVo>> getFeedbackList(PageParamRequest pageParamRequest, HttpServletRequest request) {
        User user = userService.getUserByPhone(tokenUtil.getFrontUserPhone(request));
        return CommonResult.success(feedbackService.getFeedbackList(pageParamRequest, user));
    }

    @Operation(summary = "提交反馈")
    @PostMapping("submit")
    public CommonResult<Boolean> submitFeedback(@RequestBody Feedback feedback, HttpServletRequest request) {
        User user = userService.getUserByPhone(tokenUtil.getFrontUserPhone(request));
        feedback.setUserId(user.getId());

        // 保存到系统消息
        SystemMessage systemMessage = new SystemMessage();
        systemMessage.setAdminId(-1)
                .setMessage("您已提交的关于\"" + feedback.getTitle()
                        + "\"的反馈，请等待客服回复。")
                .setReceiverId(feedback.getUserId());

        systemMessageService.save(systemMessage);

        return CommonResult.success(feedbackService.save(feedback));
    }

    @Operation(summary = "获取反馈详情")
    @GetMapping("detail")
    public CommonResult<Feedback> getFeedbackDetail(String id, HttpServletRequest request) {

        User user = userService.getUserByPhone(tokenUtil.getFrontUserPhone(request));
        // 判断是否登录
        if (ObjectUtils.isNull(user)) {
            return CommonResult.failed(ExceptionCodeEnum.UNAUTHORIZED, "还未登录");
        }

        Feedback feedback = feedbackService.getById(id);
        if (ObjectUtils.isNotNull(feedback)) {
            // 判断是否反馈本人操作
            if (feedback.getUserId().equals(user.getId())) {
                return CommonResult.success(feedback);
            } else {
                return CommonResult.failed(ExceptionCodeEnum.FORBIDDEN, "没有访问权限");
            }
        } else {
            return CommonResult.failed("获取失败");
        }
    }

}
